<script setup>
import '@/assets/common/common.scss'
import { ref } from 'vue'
import logo from '@/assets/logo.png'
import { loginService } from '@/api/user.js'
import { successMsg } from '@/utils/sendMsgUtils.js'
import { useRouter } from 'vue-router'
import { AddUser, Return } from '@icon-park/vue-next'
import { useUserState } from '@/stores/index.js'
import { encryption } from '@/utils/md5Utils.js'


const router = useRouter()
const userState = useUserState()
const user = ref({
  username: '',
  password: ''
})
// 表单数据


// 校验规则
const rules = ref({
  username: [
    { required: true, message: '用户名不能为空', trigger: 'blur' },
    { min: 3, max: 15, message: '用户名长度3-15字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '密码不能为空', trigger: 'blur' },
    { min: 6, message: '密码至少6位', trigger: 'blur' }
  ]
})

const formRef = ref()

const onSubmit = async () => {
  formRef.value?.validate(async (valid) => {
    if (valid) {
      user.value.password = encryption(user.value.password)
      const resp = await loginService(user.value)
      if (resp.code === 1) {
        userState.setToken(resp.data)
        userState.setUsername(user.value.username)
        successMsg('登陆成功')

        await router.push('/')
      }
    } else {
      console.log('验证失败')
    }
  })
}

</script>

<template>
  <div class="user-page">
    <el-link @click="router.push('/user/register')" :underline="false" class="top-link-left" type="primary">
      <add-user theme="outline" class="icon-center" :strokeWidth="6" />
      &nbsp;注册
    </el-link>
    <el-link @click="router.push('/')" :underline="false" class="top-link-right" type="primary">
      返回&nbsp;
      <return theme="outline" class="icon-center" :strokeWidth="6" />
    </el-link>
    <div class="user-image-logo">
      <el-image :src="logo" @click="router.push('/')" style="width: 300px;" />
    </div>
    <el-form
      :model="user"
      :rules="rules"
      ref="formRef"
      style="max-width: 400px"
      label-width="auto"
      class="demo-ruleForm"
    >
      <el-form-item label="用户名：" prop="username">
        <el-input placeholder="请输入你的用户名" style="width: 300px" v-model="user.username" />
      </el-form-item>
      <el-form-item label="密码：" prop="password">
        <el-input type="password" placeholder="请输入你的密码" style="width: 300px" v-model="user.password" />
      </el-form-item>
      <el-row style="margin-bottom: 15px">
        <el-col :span="5"></el-col>
        <el-col :span="5">
          <el-checkbox>记住密码</el-checkbox>
        </el-col>
        <el-col :span="5"></el-col>
        <el-col :span="5" class="icon-center">
          <el-link @click.prevent="router.push('/user/forgot')" :underline="false">忘记密码？</el-link>
        </el-col>
      </el-row>

      <div style=" text-align: center;margin-bottom: 15px">
        <el-button style="width: 300px" size="large" round type="primary" @click="onSubmit">登录</el-button>
      </div>
    </el-form>
  </div>
</template>

<style scoped>

.demo-ruleForm {
  width: 100%;
}


</style>


